#include "player.h"

void Initboard(char chessboard[length][width])
{
	for (int x = 0; x < length; x++)
	{
		for (int y = 0; y < width; y++)
		{
			chessboard[x][y] = ' ';
		}
	}
}

void Showboard(char chessboard[length][width])
{
	for (int x = 0; x < length; x++)
	{
		for (int y = 0; y < width; y++)
		{
			printf(" %c ", chessboard[x][y]);
			if (y < width - 1)
			{
				printf("|");
			}
		}
		if (x < length - 1)
		{
			printf("\n------------\n");
		}
	}
	printf("\n");
}

void player1(char chessboard[length][width])
{
	int x, y;
	while (1)
	{
		printf("请输入你的坐标:");
		scanf("%d %d", &x, &y);
		if ((x - 1 >= 0) && (x - 1 < length) && (y - 1 >= 0) && (y - 1 < width) && chessboard[x - 1][y - 1] == ' ')
		{
			chessboard[x - 1][y - 1] = '*';
			break;
		}
		else
		{
			printf("位置有误，请重新下\n");
		}
	}
}

void player2(char chessboard[length][width])
{
	while (1)
	{
		int x = rand() % length;
		int y = rand() % width;
		if (chessboard[x][y] == ' ')
		{
			chessboard[x][y] = '#';
			break;
		}
	}
}

int win(char chessboard[length][width])
{
	int z = length * width;
	for (int x = 0; x < length; x++)//横
	{
		int a = 0;
		int b = 0;
		for (int y = 0; y < width; y++)
		{
			if (chessboard[x][y] == '#')
			{
				b++;
				z--;
			}
			else if (chessboard[x][y] == '*')
			{
				a++;
				z--;
			}
		}
		if (a == length)
		{
			return 1;
		}
		if (b == length)
		{
			return 2;
		}
	}
	if (z == 0)
	{
		return 0;
	}
	for (int x = 0; x < width; x++)//竖
	{
		int a = 0;
		int b = 0;
		for (int y = 0; y < length; y++)
		{
			if (chessboard[y][x] == '#')
			{
				b++;
			}
			else if (chessboard[y][x] == '*')
			{
				a++;
			}
		}
		if (a == width)
		{
			return 1;
		}
		if (b == width)
		{
			return 2;
		}
	}
	for (int x = 0, y = 0; x < length, y < width; x++, y++)//斜
	{
		int a = 0;
		int b = 0;
		if (chessboard[x][y] == '#')
		{
			b++;
			z--;
		}
		else if (chessboard[x][y] == '*')
		{
			a++;
			z--;
		}
		if (a == width)
		{
			return 1;
		}
		if (b == width)
		{
			return 2;
		}
	}
	for (int x = 0, y = width - 1; x < length, y >= 0; x++, y--)//斜
	{
		int a = 0;
		int b = 0;
		if (chessboard[x][y] == '#')
		{
			b++;
			z--;
		}
		else if (chessboard[x][y] == '*')
		{
			a++;
			z--;
		}
		if (a == width)
		{
			return 1;
		}
		if (b == width)
		{
			return 2;
		}
	}
	return -1;
}
